/**
 *
 *  首场景 
 *
 */
class MainScreen extends BaseScreen {
    public constructor() {
        super();
        this.skinName = "resource/eui_skins/MainScreenSkin.exml";
        
    }

    public img_tipbtn:eui.Image;
    public img_btn1:eui.Image;
    public img_earth:eui.Image;
    public group_3:eui.Group;

    public img_rand_btn:eui.Image;
    public img_start_btn:eui.Image;

    public img_item4:eui.Image;
    public img_item1:eui.Image;
    public img_man:eui.Image;
    public group_tip:eui.Group;
    
    public group_loading:screenLoad;
    public rank_panl:grank;

    public group_2:eui.Group;
    public img_tip_page:eui.Image;
    public scroller_bg:eui.Scroller;
    protected childrenCreated(): void {
        super.childrenCreated();
        this.init_view();
    }

    public init_view(){
        this.img_tipbtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.show_tips_hander, this);
        //  this.img_tipbtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.start_game_hander, this);
        this.img_rand_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.rank_hander, this);
        this.img_start_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.start_game_hander, this);
        this.group_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.group3_hander, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);

        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadResourceLoadComplete, this);
		RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);

        pub.SignalManager.instance.addSignal(pub.SignalDefin.CLOSE_RANK,this.close_rank,this);

        egret.Tween.get(this.img_btn1,{"loop":true}).to({"x":450},500).wait(100).to({"x":459},300);

        this.img_item4.y = -this.img_item4.height;
        egret.Tween.get(this.img_item4).to({"y":238},500).wait(100).to({"y":218},300);

        this.img_man.x = this.width + this.img_man.width;
        egret.Tween.get(this.img_man).to({"x":0},500).wait(100).to({"x":65},300);

        this.img_item1.x = -this.img_item1.width;
        egret.Tween.get(this.img_item1).to({"x":20},300).wait(100).to({"x":0},300);

        this.group_tip.scaleX = this.group_tip.scaleX = 0;
        egret.Tween.get(this.group_tip).to({"scaleX":1.3,"scaleY":1.3},300).wait(100).to({"scaleX":1,"scaleY":1},200);

        this.group_loading = new screenLoad();
		this.addChild(this.group_loading);
		this.group_loading.close();

        this.rank_panl.close_scene();

    }

    public show_tips_hander(e:egret.Event){
        this.group_3.visible = true;
    }

    public rank_hander(e:egret.Event){
        this.rank_panl.show_scene();

        this.img_rand_btn.visible = false;
        this.img_start_btn.visible = false;
    }

    public close_rank(){
        this.img_rand_btn.visible = true;
        this.img_start_btn.visible = true;
    }

    public start_game_hander(e:egret.Event){
        var isplay = egret.getOption("isplay");
        if(isplay && isplay == 'ok')
        {
            utils.Toast.instance.showMsg('您今天已经玩过了哦!');
            return;
        }
        
        this.loadGradeSource();
    }

    /**
     * 关闭提示框
     */
    public group3_hander(e:egret.Event){
        this.group_3.visible = false;
    }
    

    private OnEnterFrame(e: egret.Event) {
        this.img_earth.rotation +=1;
    }

    //加载下一页的资源
    private loadGradeSource(): void {
        console.log(`加载资源组scene2`);
        RES.loadGroup('scene2');
        this.group_loading.show();
    }
    //加载group结束
    private onLoadResourceLoadComplete(event: RES.ResourceEvent): void {
        console.log(`资源组scene2加载结束`);
        this.sourceLoadOver = true;
        this.changeScreen();
    }
    
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
	private onResourceProgress(event: RES.ResourceEvent): void {
		// if (!this.group_loading.visible) return;
        // if(!this.processText) return;
		// this.processText.text = Math.floor(event.itemsLoaded * 100 / event.itemsTotal) + "%";
	}

    //转换场景
    private changeScreen(): void {
        this.group_loading.close();
        this.sourceLoadOver=false;
        this.clear();
        utils.ScreenManager.instance.changeToShowScreen(2);
    }

    public clear(){
        this.img_tipbtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.show_tips_hander, this);
        this.img_rand_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.rank_hander, this);
        this.img_start_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.start_game_hander, this);
        this.group_3.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.group3_hander, this);
        this.removeEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadResourceLoadComplete, this);
		RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        pub.SignalManager.instance.removeSignal(pub.SignalDefin.CLOSE_RANK,this.close_rank,this);
        egret.Tween.removeAllTweens();
    }

    public resize(): void {
        super.resize();

        // this.group_2.y = this.height - this.group_2.height;
        // console.log('this.height1'+this.height);
        // console.log('this.height2'+this.rank_panl.height);
        
        this.scroller_bg.height = this.height;

        this.rank_panl.y = (this.height - 1028)*0.5;
        this.img_tip_page.y = (this.height - this.img_tip_page.height)*0.5;
        this['rect_help'].alpha = 0.5;
    }
}

